package com.wlzg.collectiontool.view

import android.Manifest
import android.app.Activity
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Color
import android.graphics.drawable.BitmapDrawable
import android.os.Bundle
import android.util.Log
import android.view.MotionEvent
import android.view.View
import androidx.lifecycle.lifecycleScope
import com.esri.arcgisruntime.ArcGISRuntimeEnvironment
import com.esri.arcgisruntime.data.TileCache
import com.esri.arcgisruntime.geometry.GeometryEngine
import com.esri.arcgisruntime.geometry.Point
import com.esri.arcgisruntime.geometry.PolylineBuilder
import com.esri.arcgisruntime.layers.ArcGISTiledLayer
import com.esri.arcgisruntime.mapping.ArcGISMap
import com.esri.arcgisruntime.mapping.Basemap
import com.esri.arcgisruntime.mapping.view.DefaultMapViewOnTouchListener
import com.esri.arcgisruntime.mapping.view.Graphic
import com.esri.arcgisruntime.mapping.view.GraphicsOverlay
import com.esri.arcgisruntime.mapping.view.LocationDisplay
import com.esri.arcgisruntime.symbology.PictureMarkerSymbol
import com.esri.arcgisruntime.symbology.SimpleLineSymbol
import com.esri.arcgisruntime.symbology.SimpleMarkerSymbol
import com.github.dfqin.grantor.PermissionListener
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView
import com.wlzg.collectiontool.App
import com.wlzg.collectiontool.R
import com.wlzg.collectiontool.db.AppDatabase
import com.wlzg.collectiontool.map.TianDiTuTiledLayerClass
import com.wlzg.collectiontool.model.bean.*
import com.wlzg.collectiontool.util.*
import kotlinx.android.synthetic.main.fragment_invest.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.text.NumberFormat

class InvestMapFragment : BaseMapFragment(), View.OnClickListener {
    var locationDisplay: LocationDisplay? = null
    protected val graphicsOverlay by lazy { GraphicsOverlay() }
    private var addPointModel = false
    private var addLineModel = false
    private var tongbuModel = false
    private var jiaoyanModel = false
    private var sousuoModel = false
    private var celiangModel = false
    private var pvTimeU: TimePickerUtil? = null
    private var souSuoPop: BasePopupView? = null
    private var guandianPop: BasePopupView? = null
    private var guanxianPop: BasePopupView? = null
    private var tbdiaPop: BasePopupView? = null
    private var jiaoyanPop: BasePopupView? = null
    private val linePoints by lazy { ArrayList<Point>() }
    private val linePointss by lazy { ArrayList<String>() }
    var qidianPoint: Point? = null
    var zdianPoint: Point? = null
    var qidianBh: String? = ""
    var zdianBh: String? = ""
    var pointGraphic: Graphic? = null
    var lineGraphic: Graphic? = null
    var gxGraphic: Graphic? = null
    var guandianDia: GuandianDia? = null
    var guanxianDia: GuanxianDia? = null
    var tbDia:TongbuDia?=null
    var gdPoint: Point? = null
    override fun getLayoutId(): Int =
        R.layout.fragment_invest

    override fun initView(container: View?, savedInstanceState: Bundle?) {
        super.initView(container, savedInstanceState)
        initMap()
        souSuoPop = XPopup.Builder(context).dismissOnTouchOutside(false).offsetY(80)
            .offsetX(600)
            .borderRadius(15f)
            .asCustom(SousuoDia(requireContext(), this))
        jiaoyanPop = XPopup.Builder(context).dismissOnTouchOutside(false)
            .offsetX(50)
            .offsetY(30)
            .asCustom(JiaoheDia(requireContext(), this))
        initguandianDia(null, 1)
        initguanxianDia(null)
        initTongbudia()
        guandian.setOnClickListener(this)
        guanxian.setOnClickListener(this)
        sousuo.setOnClickListener(this)
        jiaoyan.setOnClickListener(this)
        tongbu.setOnClickListener(this)
        celiang.setOnClickListener(this)
        location.setOnClickListener(this)
        scaleup.setOnClickListener(this)
        scaledowm.setOnClickListener(this)
        initListener()
    }

    private fun initMap() {
        ArcGISRuntimeEnvironment.setLicense("runtimelite,1000,rud4449636536,none,NKMFA0PL4S0DRJE15166")
        mapView.isAttributionTextVisible = false
        var basemap: Basemap? = null
        if (!WifiUtil.isNetworkConnected(App.mContext)) {
            val path = FileUtil.getSDPath() + "/map/yudu_basemap.tpk"
            val titleCache = TileCache(path)
            val arcGISTiledLayer = ArcGISTiledLayer(titleCache)
            basemap = Basemap(arcGISTiledLayer)
        } else {
            //默认底图
            val webTiledLayer =
                TianDiTuTiledLayerClass.CreateTianDiTuTiledLayer(
                    TianDiTuTiledLayerClass.LayerType.TIANDITU_VECTOR_MERCATOR
                )
            //标注
            val webTiledLayerLable =
                TianDiTuTiledLayerClass.CreateTianDiTuTiledLayer(
                    TianDiTuTiledLayerClass.LayerType.TIANDITU_VECTOR_MERCATOR_LABLE
                )
            basemap = Basemap()
            basemap.baseLayers.add(webTiledLayer)
            basemap.baseLayers.add(webTiledLayerLable)
        }
        val arcGISMap = ArcGISMap(basemap)
        mapView.map = arcGISMap
        mapView.graphicsOverlays.add(graphicsOverlay)
        PermissionUtil.requestPermission(
            context as Activity?, arrayOf<String>(
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.CAMERA

            ), object : PermissionListener {
                override fun permissionGranted(permission: Array<out String>) {
                    locationDisplay = mapView.locationDisplay
                    locationDisplay?.isShowLocation = true
                    locationDisplay?.isShowPingAnimation = true
                    locationDisplay?.isShowAccuracy = true
                    locationDisplay?.autoPanMode = LocationDisplay.AutoPanMode.OFF
                    locationDisplay?.startAsync()
                }

                override fun permissionDenied(permission: Array<out String>) {

                }

            })


//        tvScale.text = "${mapView.mapScale}"
    }

    fun initTongbudia(){
        tbDia= TongbuDia(requireContext(),this)
        tbdiaPop =
            XPopup.Builder(context).dismissOnTouchOutside(false).dismissOnBackPressed(false)
                .borderRadius(15.0f)
                .asCustom(tbDia)
    }

    fun initguandianDia(t: Any?, type: Int) {
        guandianDia = GuandianDia(requireContext(), this, t, type)
        guandianPop =
            XPopup.Builder(context).dismissOnTouchOutside(false).dismissOnBackPressed(false)
                .borderRadius(15.0f)
                .asCustom(guandianDia)
    }

    fun initguanxianDia(t: PsGuanxian?) {
        guanxianDia = GuanxianDia(requireContext(), this, t)
        guanxianPop =
            XPopup.Builder(context).dismissOnTouchOutside(false).dismissOnBackPressed(false)
                .borderRadius(15.0f)
                .asCustom(guanxianDia)
    }

    private fun switchModel(view: View) {
        clearModel()
        guandian.isSelected = false
        guanxian.isSelected = false
        tongbu.isSelected = false
        jiaoyan.isSelected = false
        sousuo.isSelected = false
        celiang.isSelected = false
        view.isSelected = true

    }

    override fun onClick(p0: View?) {
        when (p0?.id) {
            R.id.guandian -> {
                switchModel(guandian)
                addPointModel = true
            }
            R.id.guanxian -> {
                switchModel(guanxian)
                addLineModel = true
                linePoints.clear()
                linePointss.clear()
            }
            R.id.tongbu -> {
                switchModel(tongbu)
                tongbuModel = true
                if (!tbdiaPop?.isShow!!) {
                    initTongbudia()
                    tbdiaPop?.show()
                }
            }
            R.id.jiaoyan -> {
                switchModel(jiaoyan)
                jiaoyanModel = true
                if (!jiaoyanPop?.isShow!!) {
                    jiaoyanPop?.show()
                }
            }
            R.id.sousuo -> {
                switchModel(sousuo)
                sousuoModel = true
                if (!souSuoPop?.isShow!!) {
                    souSuoPop?.show()
                }

            }
            R.id.celiang -> {
                switchModel(celiang)
                celiangModel = true

            }
            R.id.location -> {
                if (locationDisplay?.autoPanMode == LocationDisplay.AutoPanMode.OFF) {
                    locationDisplay?.autoPanMode = LocationDisplay.AutoPanMode.RECENTER
                }

            }
            R.id.scaleup -> {
                mapView.setViewpointScaleAsync(mapView.mapScale * 0.5)

            }
            R.id.scaledowm -> {
                mapView.setViewpointScaleAsync(mapView.mapScale * 2)

            }
        }
    }

    private fun initListener() {
        mapView.onTouchListener = object : DefaultMapViewOnTouchListener(context, mapView) {
            override fun onSingleTapConfirmed(e: MotionEvent): Boolean {
                if (mapView.callout != null && mapView.callout.isShowing) {
                    mapView.callout.dismiss()
                }
                if (addPointModel) {
                    val point =
                        android.graphics.Point(e.x.toInt(), e.y.toInt())
                    queryByPointAndShow(point)
                } else if (addLineModel) {
                    val point =
                        android.graphics.Point(e.x.toInt(), e.y.toInt())
                    queryLineAndShow(point)
                }

                return super.onSingleTapConfirmed(e)
            }

            override fun onDoubleTap(e: MotionEvent?): Boolean {
                return super.onDoubleTap(e)
            }
        }
    }

    fun clearModel() {
        addPointModel = false
        addLineModel = false
        tongbuModel = false
        jiaoyanModel = false
        sousuoModel = false
    }

    fun queryPointLayer(point: android.graphics.Point) {
        val operationalLayers = mapView.map.operationalLayers
        if (operationalLayers != null && operationalLayers.size > 0) {
            val listListenableFuture = mapView.identifyLayerAsync(
                operationalLayers[operationalLayers.lastIndex],
                point,
                15.0,
                false,
                -1
            )
            listListenableFuture.addDoneListener {
                val elements = listListenableFuture.get().elements
                for (element in elements) {
                    if (element.attributes.containsKey("type")) {

                    } else if (element.attributes.containsKey("id")) {

                    }

                }
            }
        }
    }

    fun queryByPoint(point: android.graphics.Point, type: Int) {
        val listListenableFuture = mapView.identifyGraphicsOverlayAsync(
            mapView.graphicsOverlays[0],
            point,
            15.0,
            false
        )
        listListenableFuture.addDoneListener {
            val graphics = listListenableFuture.get().graphics
            if (graphics.size > 0) {
                val graphic = graphics[0]
                var mbitmap: Bitmap? = null
                when (type) {
                    1 -> {
                        mbitmap =
                            BitmapFactory.decodeResource(resources, R.mipmap.huafenchi)
                    }
                    2 -> {
                        mbitmap =
                            BitmapFactory.decodeResource(resources, R.mipmap.huafenchi)
                    }
                    else -> {
                        mbitmap = BitmapFactory.decodeResource(resources, R.mipmap.huafenchi)
                    }


                }
                val bitmapDrawable = BitmapDrawable(resources, mbitmap)
                val pictureMarkerSymbol = PictureMarkerSymbol(bitmapDrawable)
                graphic?.symbol = pictureMarkerSymbol
                pictureMarkerSymbol.loadAsync()

            }

        }
    }


//    fun queryLineAndShow(point: android.graphics.Point) {
//        val listListenableFuture = mapView.identifyGraphicsOverlayAsync(
//            mapView.graphicsOverlays[0],
//            point,
//            15.0,
//            false
//        )
//        listListenableFuture.addDoneListener {
//            val graphics = listListenableFuture.get().graphics
//            if (graphics.size <= 0) {
//                val point1: Point = mapView.screenToLocation(point)
//                linePoints.add(point1)
//                if (linePoints.size == 1) {
//                    val pointSymbol = SimpleMarkerSymbol(
//                        SimpleMarkerSymbol.Style.SQUARE,
//                        Color.RED,
//                        2F
//                    )
//                    gxGraphic = Graphic(point1, pointSymbol)
//                    graphicsOverlay.graphics.add(gxGraphic)
//                } else {
//                    graphicsOverlay.graphics.remove(gxGraphic)
//                }
//                if (linePoints.size <= 2) {
//                    val lineGeometry = PolylineBuilder(mapView.spatialReference)
//                    lineGeometry.addPoints(linePoints)
//                    val lineSymbol = SimpleLineSymbol(
//                        SimpleLineSymbol.Style.SOLID,
//                        Color.RED,
//                        4F
//                    )
//                    val length = GeometryEngine.length(lineGeometry.toGeometry())
//                    val numberFormat = NumberFormat.getInstance()
//                    numberFormat.maximumFractionDigits = 2
//                    val s = numberFormat.format(length)
//                    lineGraphic = Graphic(lineGeometry.toGeometry(), lineSymbol)
//                    graphicsOverlay.graphics.add(lineGraphic)
//                    if (linePoints.size == 2) {
//                        if (!guanxianPop?.isShow!!) {
//                            initguanxianDia(null)
//                            qidianPoint = linePoints[0]
//                            zdianPoint = linePoints[1]
//                            guanxianPop?.show()
//                            linePoints.clear()
//                        }
//                    }
//                }
//            } else {
//                val graphic = graphics[0]
//                if (graphic.attributes.containsKey("id")) {
//                    val id = graphic.attributes.get("id")
//                    lifecycleScope.launch(context = Dispatchers.Main) {
//                        val gx: PsGuanxian
//                        withContext(Dispatchers.IO) {
//                            gx = AppDatabase.getInstance().userDao().getgx(id as Long)
//                        }
//                        if (gx != null && !guanxianPop?.isShow!!) {
//                            initguanxianDia(gx)
//                            guanxianPop?.show()
//                        }
//                    }
//                }
//
//
//            }
//        }
//    }


    fun queryLineAndShow(point: android.graphics.Point) {
        val listListenableFuture = mapView.identifyGraphicsOverlayAsync(
            mapView.graphicsOverlays[0],
            point,
            15.0,
            false
        )
        listListenableFuture.addDoneListener {
            val graphics = listListenableFuture.get().graphics
            if (graphics.size > 0) {
                val graphic = graphics[0]
                if (graphic.attributes.containsKey("type")) {
                    val type = graphic.attributes.get("type")
                    if (type == -1) {
                        if (graphic.attributes.containsKey("id")) {
                            val id = graphic.attributes.get("id")
                            lifecycleScope.launch(context = Dispatchers.Main) {
                                val gx: PsGuanxian
                                withContext(Dispatchers.IO) {
                                    gx = AppDatabase.getInstance().userDao().getgx(id as Long)
                                }
                                if (gx != null && !guanxianPop?.isShow!!) {
                                    initguanxianDia(gx)
                                    guanxianPop?.show()
                                }
                            }
                        }
                    } else {
                        if (graphic.attributes.containsKey("bh")) {
                            val bh = graphic.attributes.get("bh")
                                val point1: Point = graphic.geometry as Point
                                linePoints.add(point1)
                                linePointss.add(bh as String)
                                if (linePoints.size == 1) {
                                    val pointSymbol = SimpleMarkerSymbol(
                                        SimpleMarkerSymbol.Style.SQUARE,
                                        Color.RED,
                                        2F
                                    )
                                    gxGraphic = Graphic(point1, pointSymbol)
                                    graphicsOverlay.graphics.add(gxGraphic)
                                } else {
                                    graphicsOverlay.graphics.remove(gxGraphic)
                                }
                                if (linePoints.size <= 2) {
                                    val lineGeometry = PolylineBuilder(mapView.spatialReference)
                                    lineGeometry.addPoints(linePoints)
                                    val lineSymbol = SimpleLineSymbol(
                                        SimpleLineSymbol.Style.SOLID,
                                        Color.RED,
                                        4F
                                    )
                                    val length = GeometryEngine.length(lineGeometry.toGeometry())
                                    val numberFormat = NumberFormat.getInstance()
                                    numberFormat.maximumFractionDigits = 2
                                    val s = numberFormat.format(length)
                                    lineGraphic = Graphic(lineGeometry.toGeometry(), lineSymbol)
                                    graphicsOverlay.graphics.add(lineGraphic)


                                }
                                if (linePoints.size == 2) {
                                    if (!guanxianPop?.isShow!!) {
                                        initguanxianDia(null)
                                        qidianPoint = linePoints[0]
                                        zdianPoint = linePoints[1]
                                        qidianBh =linePointss[0]
                                        zdianBh=linePointss[1]
                                        guanxianPop?.show()
                                        linePoints.clear()
                                        linePointss.clear()
                                    }

                            }
                        }

                    }
                }

            }
        }
    }

    fun queryByPointAndShow(point: android.graphics.Point) {
        val listListenableFuture = mapView.identifyGraphicsOverlayAsync(
            mapView.graphicsOverlays[0],
            point,
            15.0,
            false
        )
        listListenableFuture.addDoneListener {
            val graphics = listListenableFuture.get().graphics
            if (graphics.size <= 0) {
                val point1: Point = mapView.screenToLocation(point)
                gdPoint = point1
                // 生成一个红色圆形标识
                val pointSymbol = SimpleMarkerSymbol(
                    SimpleMarkerSymbol.Style.CIRCLE,
                    Color.RED,
                    16F
                )
                pointGraphic = Graphic(point1, pointSymbol)
                graphicsOverlay.graphics.add(pointGraphic)
                if (!guandianPop?.isShow!!) {
                    initguandianDia(null, 0)
                    guandianPop?.show()
                }
                return@addDoneListener
            } else {
                val graphic = graphics[0]
                if (graphic.attributes.containsKey("type")) {
                    val type = graphic.attributes.get("type")
                    when (type) {
                        1 -> {
                            val id = graphic.attributes.get("id")
                            Log.d("ssssdff", "ssid" + id)
                            lifecycleScope.launch(context = Dispatchers.Main) {
                                val psJin: PsJin
                                withContext(Dispatchers.IO) {
                                    psJin =
                                        AppDatabase.getInstance().userDao().getPsjin(id as Long)
                                }
                                if (psJin != null && !guandianPop?.isShow!!) {
                                    initguandianDia(psJin as Any, 1)

                                    guandianPop?.show()
                                }
                            }
                        }

                        2 -> {
                            val id = graphic.attributes.get("id")
                            lifecycleScope.launch(context = Dispatchers.Main) {
                                val psysk: Psyushuikou
                                withContext(Dispatchers.IO) {
                                    psysk =
                                        AppDatabase.getInstance().userDao().getPsysk(id as Long)
                                }
                                if (psysk != null && !guandianPop?.isShow!!) {
                                    initguandianDia(psysk as Any, 2)

                                    guandianPop?.show()
                                }
                            }
                        }
                        3 -> {
                            val id = graphic.attributes.get("id")
                            lifecycleScope.launch(context = Dispatchers.Main) {
                                val pfk: Pspfk
                                withContext(Dispatchers.IO) {
                                    pfk =
                                        AppDatabase.getInstance().userDao().getPfk(id as Long)
                                }
                                if (pfk != null && !guandianPop?.isShow!!) {
                                    initguandianDia(pfk as Any, 3)

                                    guandianPop?.show()
                                }
                            }
                        }
                        4 -> {
                            val id = graphic.attributes.get("id")
                            lifecycleScope.launch(context = Dispatchers.Main) {
                                val lg: PsLiguan
                                withContext(Dispatchers.IO) {
                                    lg =
                                        AppDatabase.getInstance().userDao().getLg(id as Long)
                                }
                                if (lg != null && !guandianPop?.isShow!!) {
                                    initguandianDia(lg as Any, 4)

                                    guandianPop?.show()
                                }
                            }
                        }
                        5 -> {
                            val id = graphic.attributes.get("id")
                            lifecycleScope.launch(context = Dispatchers.Main) {
                                val hfc: PsHuafenchi
                                withContext(Dispatchers.IO) {
                                    hfc =
                                        AppDatabase.getInstance().userDao().gethfc(id as Long)
                                    Log.d("hhhhh", "bj" + hfc.septictacode)
                                }
                                if (hfc != null && !guandianPop?.isShow!!) {
                                    initguandianDia(hfc as Any, 5)

                                    guandianPop?.show()
                                }
                            }
                        }


                    }
                }


            }
        }
    }

    fun queryPointbycode(code: String) {
        lifecycleScope.launch(context = Dispatchers.Main) {
            var jin: PsJin
            var ysk: Psyushuikou
            var pfk: Pspfk
            var lg: PsLiguan
            var hfc: PsHuafenchi
            withContext(Dispatchers.IO) {
//                jin = AppDatabase.getInstance().userDao().quePsjinbyCode(code)
//                ysk = AppDatabase.getInstance().userDao().queyskbyCode(code)
//                pfk = AppDatabase.getInstance().userDao().quePspfkbyCode(code)
//                lg = AppDatabase.getInstance().userDao().quelgbyCode(code)
//                hfc = AppDatabase.getInstance().userDao().quehfcbyCode(code)


                var bitmap: Bitmap? = null
                var mgra: Graphic? = null

                val gras = graphicsOverlay.graphics
//                for (gra in gras) {
//                    if (gra.attributes["bh"] == jin?.jinId) {
//                        Log.d("qweer","jid"+jin?.manholecode)
//                        mgra = gra
//                        bitmap = BitmapFactory.decodeResource(resources, R.mipmap.jgaol)
////                        when (gra.attributes["type1"]) {
////                            1 -> {
////                                bitmap = BitmapFactory.decodeResource(resources, R.mipmap.yushuikou)
////                            }
////
////                        }
//                    } else if (gra.attributes["id"] == ysk?.yskId) {
//                        Log.d("qweer","jid"+jin?.manholecode)
//                        mgra = gra
//                        bitmap = BitmapFactory.decodeResource(resources, R.mipmap.yskgl)
//                    } else if (gra.attributes["id"] == pfk?.pfkid) {
//                        Log.d("qweer","werert3")
//                        mgra = gra
//                        bitmap = BitmapFactory.decodeResource(resources, R.mipmap.pfkgl)
//
//                    } else if (gra.attributes["id"] == lg?.lgid) {
//                        Log.d("qweer","werert4")
//                        mgra = gra
//                        bitmap = BitmapFactory.decodeResource(resources, R.mipmap.lggl)
//
//                    } else if (gra.attributes["id"] == hfc?.hfcId) {
//                        Log.d("qweer","werert5")
//                        mgra = gra
//                        bitmap = BitmapFactory.decodeResource(resources, R.mipmap.hfcgl)
//                    }
//
//                }
                for (gra in gras) {
                    if ((gra.attributes["bh"] as? String)?.equals(code) == true) {
                        val type = gra.attributes["type"]
                        when (type) {
                            1 -> {
                                bitmap = BitmapFactory.decodeResource(resources, R.mipmap.jgaol)
                            }
                            2 -> {
                                bitmap = BitmapFactory.decodeResource(resources, R.mipmap.yskgl)
                            }
                            3 -> {
                                bitmap = BitmapFactory.decodeResource(resources, R.mipmap.pfkgl)
                            }
                            4 -> {
                                bitmap = BitmapFactory.decodeResource(resources, R.mipmap.lggl)
                            }
                            5 -> {
                                bitmap = BitmapFactory.decodeResource(resources, R.mipmap.hfcgl)
                            }
                        }
                        mgra = gra
                    }
                }
                if (bitmap != null) {
                    val bitmapDrawable = BitmapDrawable(resources, bitmap)
                    val pictureMarkerSymbol = PictureMarkerSymbol(bitmapDrawable)
                    mgra?.symbol = pictureMarkerSymbol
                    pictureMarkerSymbol.loadAsync()
                }
                if (mgra != null) {
                    val p = mgra.geometry as? Point
                    if (p != null) {
                        mapView.setViewpointCenterAsync(p)
                    }
                }
            }


        }
    }

    fun onsaveLine(obj: PsGuanxian) {
        lifecycleScope.launch(context = Dispatchers.Main) {
            withContext(Dispatchers.IO) {
                obj?.let {
                    it.updateTime=DateUtil.getCurData()
                    val ids = AppDatabase.getInstance().userDao().insertgx(it)
                    lineGraphic!!.attributes["type"] = -1
                    lineGraphic!!.attributes["id"] = ids
//                    lineGraphic!!.attributes["bh"]=it.
                }
            }

        }
    }

    fun ondeleteGuanxin(obj: Any?) {
        graphicsOverlay.graphics.remove(lineGraphic)
        obj?.let {
            obj as PsGuanxian
            lifecycleScope.launch(context = Dispatchers.IO) {
                AppDatabase.getInstance().userDao().deleteGx(obj)
            }
        }
    }

    fun ondelete(sSleixin: Constants.SSleixin, obj: Any?) {
        graphicsOverlay.graphics.remove(pointGraphic)
        obj?.let {
            when (sSleixin) {
                Constants.SSleixin.JIANCHAJIN -> {
                    lifecycleScope.launch(context = Dispatchers.IO) {
                        it as PsJin
                        AppDatabase.getInstance().userDao().deletejcj(it)

                    }
                }

                Constants.SSleixin.YUSHUIKOU -> {
                    lifecycleScope.launch(context = Dispatchers.IO) {
                        it as Psyushuikou
                        AppDatabase.getInstance().userDao().deleteysk(it)

                    }
                }


                Constants.SSleixin.PAISHUIKOU -> {
                    lifecycleScope.launch(context = Dispatchers.IO) {
                        it as Pspfk
                        AppDatabase.getInstance().userDao().deletepfk(it)

                    }
                }


                Constants.SSleixin.LIGUAN -> {
                    lifecycleScope.launch(context = Dispatchers.IO) {
                        it as PsLiguan
                        AppDatabase.getInstance().userDao().deletelg(it)

                    }
                }


                Constants.SSleixin.HUAFENCHI -> {
                    lifecycleScope.launch(context = Dispatchers.IO) {
                        it as PsHuafenchi
                        AppDatabase.getInstance().userDao().deletehfc(it)

                    }
                }


            }
        }

    }

    fun onSave(sSleixin: Constants.SSleixin, obj: Any, isUpdate: Boolean, jintype: Int) {
        when (sSleixin) {
            Constants.SSleixin.JIANCHAJIN -> {
                obj as PsJin
                lifecycleScope.launch(context = Dispatchers.Main) {
                    withContext(Dispatchers.IO) {
                        obj.updateTime=DateUtil.getCurData()
                        val ids = AppDatabase.getInstance().userDao().insertPsjin(obj)

                        if (!isUpdate) {
                            var mbitmap: Bitmap?
                            when (jintype) {
                                0 -> {
                                    mbitmap =
                                        BitmapFactory.decodeResource(resources, R.mipmap.ysj)
                                    pointGraphic!!.attributes["type1"] = 1
                                }
                                1 -> {
                                    mbitmap =
                                        BitmapFactory.decodeResource(resources, R.mipmap.wsj)
                                    pointGraphic!!.attributes["type1"] = 2
                                }
                                2 -> {
                                    mbitmap =
                                        BitmapFactory.decodeResource(resources, R.mipmap.hlj)
                                    pointGraphic!!.attributes["type1"] = 3
                                }
                                else -> {
                                    mbitmap =
                                        BitmapFactory.decodeResource(resources, R.mipmap.ysj)
                                    pointGraphic!!.attributes["type1"] = 1
                                }
                            }

                            val bitmapDrawable = BitmapDrawable(resources, mbitmap)
                            val pictureMarkerSymbol = PictureMarkerSymbol(bitmapDrawable)
                            pointGraphic?.symbol = pictureMarkerSymbol
                            pictureMarkerSymbol.loadAsync()
                            pointGraphic!!.attributes["type"] = 1
                            pointGraphic!!.attributes["id"] = ids
                            pointGraphic!!.attributes["bh"] = obj.manholecode
                            pictureMarkerSymbol.addDoneLoadingListener {
                            }
                        }
                    }
                }

            }
            Constants.SSleixin.YUSHUIKOU -> {
                obj as Psyushuikou
                lifecycleScope.launch(context = Dispatchers.Main) {
                    withContext(Dispatchers.IO) {
                        obj.updateTime=DateUtil.getCurData()
                        val ids = AppDatabase.getInstance().userDao().insertPsysk(obj)

                        if (!isUpdate) {
                            val bitmap = BitmapFactory.decodeResource(resources, R.mipmap.ysk)
                            val bitmapDrawable = BitmapDrawable(resources, bitmap)
                            val pictureMarkerSymbol = PictureMarkerSymbol(bitmapDrawable)
                            pointGraphic?.symbol = pictureMarkerSymbol
                            pictureMarkerSymbol.loadAsync()
                            pointGraphic!!.attributes["type"] = 2
                            pointGraphic!!.attributes["id"] = ids
                            pointGraphic!!.attributes["bh"] = obj?.combcode
                            pictureMarkerSymbol.addDoneLoadingListener {
                            }
                        }
                    }
                }

            }
            Constants.SSleixin.PAISHUIKOU -> {
                obj as Pspfk
                lifecycleScope.launch(context = Dispatchers.Main) {
                    withContext(Dispatchers.IO) {
                        obj.updateTime=DateUtil.getCurData()
                        val ids = AppDatabase.getInstance().userDao().insertPsk(obj)

                        if (!isUpdate) {
                            val bitmap = BitmapFactory.decodeResource(resources, R.mipmap.pfk)
                            val bitmapDrawable = BitmapDrawable(resources, bitmap)
                            val pictureMarkerSymbol = PictureMarkerSymbol(bitmapDrawable)
                            pointGraphic?.symbol = pictureMarkerSymbol
                            pictureMarkerSymbol.loadAsync()
                            pointGraphic!!.attributes["type"] = 3
                            pointGraphic!!.attributes["id"] = ids
                            pointGraphic!!.attributes["bh"] = obj?.outfallcode
                            pictureMarkerSymbol.addDoneLoadingListener {
                            }
                        }
                    }
                }

            }
            Constants.SSleixin.LIGUAN -> {
                obj as PsLiguan
                lifecycleScope.launch(context = Dispatchers.Main) {
                    withContext(Dispatchers.IO) {
                        obj.updateTime=DateUtil.getCurData()
                        val ids = AppDatabase.getInstance().userDao().insertLg(obj)

                        if (!isUpdate) {
                            val bitmap = BitmapFactory.decodeResource(resources, R.mipmap.lg)
                            val bitmapDrawable = BitmapDrawable(resources, bitmap)
                            val pictureMarkerSymbol = PictureMarkerSymbol(bitmapDrawable)
                            pointGraphic?.symbol = pictureMarkerSymbol
                            pictureMarkerSymbol.loadAsync()
                            pointGraphic!!.attributes["type"] = 4
                            pointGraphic!!.attributes["id"] = ids
                            pointGraphic!!.attributes["bh"] = obj?.risercode
                            pictureMarkerSymbol.addDoneLoadingListener {
                            }
                        }
                    }
                }
            }
            Constants.SSleixin.HUAFENCHI -> {
                obj as PsHuafenchi
                lifecycleScope.launch(context = Dispatchers.Main) {
                    withContext(Dispatchers.IO) {
                        obj.updateTime=DateUtil.getCurData()
                        Log.d("hhhhh", "bjs" + obj.septictacode)
                        val ids = AppDatabase.getInstance().userDao().inserthfc(obj)

                        if (!isUpdate) {
                            val bitmap = BitmapFactory.decodeResource(resources, R.mipmap.hfc)
                            val bitmapDrawable = BitmapDrawable(resources, bitmap)
                            val pictureMarkerSymbol = PictureMarkerSymbol(bitmapDrawable)
                            pointGraphic?.symbol = pictureMarkerSymbol
                            pictureMarkerSymbol.loadAsync()
                            pointGraphic!!.attributes["type"] = 5
                            pointGraphic!!.attributes["id"] = ids
                            pointGraphic!!.attributes["bh"] = obj?.septictacode
                            pictureMarkerSymbol.addDoneLoadingListener {
                            }
                        }
                    }
                }
            }
        }
    }


    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
//        if(hidden){
//
//            mapView.visibility=View.GONE
//        }
    }
}